/*
 * @PageName:
 * @Description: 表格筛选相关方法
 * @FilePath: /glm-mes-pc/src/utils/useFilterManagement.js
 */
import { ref } from 'vue'
export function useFilterManagement() {
	/* 筛选 */
	const filterRefs = ref([])
	/* 当前选中的筛选列 */
	const activeFilter = ref(null)
	/* 存储筛选列配置 */
	const storageFilterObject = ref({})

	const setActiveFilter = (index) => {
		activeFilter.value = index
	}

	/* 设置不同的ref，for循环生成的ref需要是一个数组 */
	const setFilterRef = (el, index) => {
		if (el) {
			filterRefs.value[index] = el
		}
	}

	/**
	 * @description: 更新筛选列数组参数
	 * @event:
	 * @param {Object} target 格式例子：{supplier: ['', '', '']}
	 * @return {*}
	 */
	const updateStorageFilterObject = (target) => {
		storageFilterObject.value[Object.keys(target)[0]] = target[Object.keys(target)[0]]
	}

	/* 从存储中移除当前筛选列的配置 */
	const removeStorageFilterObject = (beforeName) => {
		delete storageFilterObject.value[beforeName]
	}

	/* 清空存储的筛选项集合 */
	const clearStorageFilterObject = () => {
		storageFilterObject.value = {}
	}

	/* 判断如果点击的区域不是筛选框，则隐藏筛选框 */
	const showSinglePopover = (event, ref) => {
		const activeElement = document.querySelector('.popover-diy')
		if (activeElement && !activeElement.contains(event.target)) {
			if (ref) {
				ref.closeActiveFilter()
			}
		}
	}

	return {
		filterRefs,
		activeFilter,
		storageFilterObject,
		setActiveFilter,
		setFilterRef,
		updateStorageFilterObject,
		removeStorageFilterObject,
		clearStorageFilterObject,
		showSinglePopover
	}
}
